package com.tkjt.pms.modules.kpiadmin.service.impl;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.tkjt.pms.common.entity.*;
import com.tkjt.pms.modules.common.mapper.*;
import com.tkjt.pms.modules.scoring.model.ScoreDatagridResponseModel;
import com.tkjt.pms.server.common.DepartUserChangeService;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.system.mapper.SysUserMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tkjt.pms.common.utils.DepartCode;
import com.tkjt.pms.common.utils.DoubleUtil;
import com.tkjt.pms.common.utils.EsDateUtils;
import com.tkjt.pms.common.utils.InfoAttrUtil;
import com.tkjt.pms.common.utils.MyBeanUtils;
import com.tkjt.pms.common.utils.OrgGroupUtil;
import com.tkjt.pms.common.utils.TkGradeUtil;
import com.tkjt.pms.common.vos.TkIndexValueForScoreVO;
import com.tkjt.pms.modules.common.page.PmsWeightPage;
import com.tkjt.pms.modules.gm.mapper.TkGmMapper;
import com.tkjt.pms.modules.kpiadmin.mapper.DataGridMapper;
import com.tkjt.pms.modules.kpiadmin.service.KpiDataBizService;
import com.tkjt.pms.modules.kpiadmin.vo.DatagridVO;
import com.tkjt.pms.modules.manageWeight.mapper.WeightLogMapper;
import com.tkjt.pms.modules.manageWeight.mapper.WeightMapper;
import com.tkjt.pms.server.common.DepartAndInfoTool;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
@Transactional
public abstract class KpiDataBizServiceBase implements KpiDataBizService {
    @Autowired
    DataGridMapper dataGridMapper;
    @Autowired
    TkIndexValueMapper tkIndexValueMapper;
    @Autowired
    WeightMapper weightMapper;
    @Autowired
    TkIndexValueAuditMapper tkIndexValueAuditMapper;
    @Autowired
    TkIndexValueAdjustMapper tkIndexValueAdjustMapper;
    @Autowired
    TkIndexReportMapper tkIndexReportMapper;
    @Autowired
    TkIndexInfoMapper tkIndexInfoMapper;
    @Autowired
    PMSCommonMapper commonMapper;
    @Autowired
    WeightLogMapper weightLogMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private TkIndexValueCommentsMapper tkIndexValueCommentsMapper;
    @Autowired
    private DepartAndInfoTool departAndInfoTool;
    @Autowired
    protected TkGmMapper tkGmMapper;
    @Autowired
    private TkDepartGroupMapper tkDepartGroupMapper;
    @Autowired
    private TkIndexValueReferenceMapper tkIndexValueReferenceMapper;
    @Autowired
    private DepartUserChangeService departUserChangeService;
    @Autowired
    private TkChangeReportMapper tkChangeReportMapper;
    /**
     *
     * 动态列表表头信息
     * @return
     */
    @Override
    public List<Map<String, Object>> indexGroupConditions(List<Map<String,Object>> specialColums,String noVirtual,String dataType,String indexYearMonth) {
        //部门列表--表头信息
        List<Map<String,Object>> indexGroup = new ArrayList<>();
        //一线单位表头信息
        Map<String,Object> groupCMap = new HashMap<>();
        groupCMap.put("name","一线单位");
        groupCMap.put("code","C");
        List<Map<String,Object>> tabHeader = new ArrayList<>();
        Map<String,Object> orgParam = new HashMap<>();
        orgParam.put("noVirtual",noVirtual);
        //一线单位对应部门集合
        orgParam.put("orgGroup","C");
        List<Map<String, Object>> orgs = dataGridMapper.getOrgAndUserByOrgGroup(orgParam);
        tabHeader.addAll(specialColums);
        tabHeader.addAll(orgs);
        groupCMap.put("tabHeader",tabHeader);
        indexGroup.add(groupCMap);
        //本部单位表头信息
        Map<String,Object> groupGMap = new HashMap<>();
        groupGMap.put("name","养康本部");
        groupGMap.put("code","G");
        tabHeader = new ArrayList<>();
        //本部单位对应部门集合
        orgParam.put("orgGroup","G");
        orgs = dataGridMapper.getOrgAndUserByOrgGroup(orgParam);
        tabHeader.addAll(specialColums);
        tabHeader.addAll(orgs);
        groupGMap.put("tabHeader",tabHeader);
        indexGroup.add(groupGMap);
        return indexGroup;
    }


    /**
     *
     * 动态列表表头信息（获取镜像数据时展示往期部门负责人）
     * @return
     */
    @Override
    public List<Map<String, Object>> indexGroupConditionsForChangeUser(List<Map<String,Object>> specialColums,String noVirtual,String dataType,String indexYearMonth) {
        //部门列表--表头信息
        List<Map<String,Object>> indexGroup = new ArrayList<>();
        //一线单位表头信息
        Map<String,Object> groupCMap = new HashMap<>();
        groupCMap.put("name","一线单位");
        groupCMap.put("code","C");
        List<Map<String,Object>> tabHeader = new ArrayList<>();
        Map<String,Object> orgParam = new HashMap<>();
        orgParam.put("noVirtual",noVirtual);
        //一线单位对应部门集合
        orgParam.put("orgGroup","C");
        List<Map<String, Object>> orgs = dataGridMapper.getOrgAndUserByOrgGroup(orgParam);
        tabHeader.addAll(specialColums);
        String currentYearMonth = EsDateUtils.getIndexYearMonth(new Date());
        String currentTotalYearMonth = currentYearMonth.substring(0,4)+"01"+currentYearMonth.substring(4);
        //非当前考核数据
        if(!currentYearMonth.equals(indexYearMonth) && !currentTotalYearMonth.equals(indexYearMonth)){
            List<Map<String, Object>> newOrgs = new ArrayList<>();
            departUserChangeService.changeUserForCondition(indexYearMonth,orgs,newOrgs);
            tabHeader.addAll(newOrgs);
        }else{
            tabHeader.addAll(orgs);
        }
        groupCMap.put("tabHeader",tabHeader);
        indexGroup.add(groupCMap);
        //本部单位表头信息
        Map<String,Object> groupGMap = new HashMap<>();
        groupGMap.put("name","养康本部");
        groupGMap.put("code","G");
        tabHeader = new ArrayList<>();
        //本部单位对应部门集合
        orgParam.put("orgGroup","G");
        orgs = dataGridMapper.getOrgAndUserByOrgGroup(orgParam);
        tabHeader.addAll(specialColums);
        //非当前考核数据
        if(!currentYearMonth.equals(indexYearMonth) && !currentTotalYearMonth.equals(indexYearMonth)){
            List<Map<String, Object>> newOrgs = new ArrayList<>();
            departUserChangeService.changeUserForCondition(indexYearMonth,orgs,newOrgs);
            tabHeader.addAll(newOrgs);
        }else{
            tabHeader.addAll(orgs);
        }
        groupGMap.put("tabHeader",tabHeader);
        indexGroup.add(groupGMap);
        return indexGroup;
    }

    /**
     *
     * 权重动态列表表头信息
     * @return
     */
    @Override
    public List<Map<String, Object>> weightConditions(List<Map<String,Object>> specialColums,String noVirtual,String dataType) {
        //部门列表--表头信息
        List<Map<String,Object>> indexGroup = new ArrayList<>();
        //一线单位表头信息
        Map<String,Object> groupCMap = new HashMap<>();
        groupCMap.put("name","一线单位");
        groupCMap.put("code","C");
        List<Map<String,Object>> tabHeader = new ArrayList<>();
        Map<String,Object> orgParam = new HashMap<>();
        orgParam.put("noVirtual",noVirtual);
        //一线单位对应部门集合
        orgParam.put("orgGroup","C");
        List<Map<String, Object>> orgs = dataGridMapper.getOrgByOrgGroup(orgParam);
        tabHeader.addAll(specialColums);
        tabHeader.addAll(orgs);
        groupCMap.put("tabHeader",tabHeader);
        indexGroup.add(groupCMap);
        //本部单位表头信息
        Map<String,Object> groupGMap = new HashMap<>();
        groupGMap.put("name","养康本部");
        groupGMap.put("code","G");
        tabHeader = new ArrayList<>();
        //本部单位对应部门集合
        orgParam.put("orgGroup","G");
        orgs = dataGridMapper.getOrgByOrgGroup(orgParam);
        tabHeader.addAll(specialColums);
        tabHeader.addAll(orgs);
        groupGMap.put("tabHeader",tabHeader);
        indexGroup.add(groupGMap);
        return indexGroup;
    }

    public boolean checkYear(String searchYear){
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH );
        if(month == 0){
            year = year -1;
        }
        if(!String.valueOf(year).equals(searchYear)){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 设定目标值-列表信息
     * @return
     */
    @Override
    public Result<?> kaTargetDatagrid(DatagridVO datagridVO,String noVirtual,String dataType) {
        String indexGroup = datagridVO.getIndexGroup();
        String yearMonth = datagridVO.getIndexYearMonth();
        if(StringUtils.isEmpty(yearMonth)){
            return Result.error("参数缺失");
        }
        Integer pageNo = datagridVO.getPageNo();
        if(pageNo == null){
            return Result.error("参数缺失");
        }
        Integer pageSize = datagridVO.getPageSize();
        if(pageSize == null){
            return Result.error("参数缺失");
        }
        String searchYear = yearMonth.substring(0,4);
//        if(checkYear(searchYear)){
//            return Result.ok("该年份数据不存在");
//        }
        Map<String,Object> param = new HashMap<>();
        param.put("min", (pageNo - 1) * pageSize);
        param.put("max", pageSize);
        if(indexGroup != null){
            indexGroup = OrgGroupUtil.changeToCG(indexGroup);
            param.put("orgGroup",indexGroup);
        }
        List<String> infoAttrs = new ArrayList<>();
        infoAttrs.add(InfoAttrUtil.MU_BIAO_ZHI);
        param.put("indexAttrs",infoAttrs);
        param.put("indexCodes",datagridVO.getIndexCodes());
        List<Map<String,Object>> result = dataGridMapper.getIndexInfoMapByParamater(param);
        int count = dataGridMapper.getIndexInfoCountMapByParamater(param);
        kaFillDatagridDynamicColumnData(result,indexGroup,yearMonth,noVirtual,dataType);
        Page<Map<String,Object>> finalPage = new Page<Map<String,Object>>(pageNo, pageSize);
        finalPage.setTotal(count);
        finalPage.setCurrent(pageNo);
        finalPage.setSize(pageSize);
        int pages = count / pageSize;
        if (count % pageSize > 0) {
            pages += 1;
        }
        finalPage.setPages(pages);
        finalPage.setRecords(result);
        return Result.ok(finalPage);
    }

    /**
     * 目标值实际值填报-列表信息
     * @return
     */
    @Override
    public Result<?> kaFillDatagrid(DatagridVO datagridVO,String noVirtual,String dataType) {
        String indexGroup = datagridVO.getIndexGroup();
        String yearMonth = datagridVO.getIndexYearMonth();
        if(com.baomidou.mybatisplus.core.toolkit.StringUtils.isEmpty(yearMonth)) {
            yearMonth = EsDateUtils.getIndexYearMonth(new Date());
        }
        Integer pageNo = datagridVO.getPageNo();
        if(pageNo == null){
            return Result.error("参数缺失");
        }
        Integer pageSize = datagridVO.getPageSize();
        if(pageSize == null){
            return Result.error("参数缺失");
        }
        String searchYear = yearMonth.substring(0,4);
//        if(checkYear(searchYear)){
//            return Result.ok("该年份数据不存在");
//        }
        Map<String,Object> param = new HashMap<>();
        param.put("min", (pageNo - 1) * pageSize);
        param.put("max", pageSize);
        if(indexGroup != null){
            String orgGroup = OrgGroupUtil.changeToCG(indexGroup);
            param.put("orgGroup",orgGroup);
        }
        param.put("indexValueEdit",datagridVO.getIndexValueEdit());
        if(datagridVO.getIndexCodes() != null && datagridVO.getIndexCodes().size() > 0){
            param.put("indexCodes",datagridVO.getIndexCodes());
        }
        if(datagridVO.getIndexTypes() != null && datagridVO.getIndexTypes().size() > 0){
            param.put("indexTypes",datagridVO.getIndexTypes());
        }
        if(datagridVO.getIndexAttrs() != null && datagridVO.getIndexAttrs().size() > 0){
            List<String> attrs = datagridVO.getIndexAttrs();
            List<String> attrsFinal = new ArrayList<>();
            for(String attr : attrs){
                attrsFinal.add(attr);
                if(attr.equals(InfoAttrUtil.SHI_JI_ZHI)){
                    attrsFinal.add(InfoAttrUtil.SHU_LIANG);
                }
                if(attr.equals(InfoAttrUtil.DE_FEN)){
                    attrsFinal.add(InfoAttrUtil.XIAOXIANG_DE_FEN);
                    attrsFinal.add(InfoAttrUtil.SHUANG_XIANG_PING_FEN);
                }
            }
            param.put("indexAttrs",attrsFinal);
        }
        //根据indexCode 和 indexValueCode排序
        List<Map<String,Object>> result = dataGridMapper.getIndexInfoMapByParamater(param);
        int count = dataGridMapper.getIndexInfoCountMapByParamater(param);
        kaFillDatagridDynamicColumnData(result,indexGroup,yearMonth,noVirtual,dataType);
        Page<Map<String,Object>> finalPage = new Page<Map<String,Object>>(pageNo, pageSize);
        finalPage.setTotal(count);
        finalPage.setCurrent(pageNo);
        finalPage.setSize(pageSize);
        int pages = count / pageSize;
        if (count % pageSize > 0) {
            pages += 1;
        }
        finalPage.setPages(pages);
        finalPage.setRecords(result);
        return Result.ok(finalPage);
    }

    /**
     * 绩效计算审核-列表信息
     * @return
     */
    @Override
    public Result<?> kaMgrDatagrid(DatagridVO datagridVO,String noVirtual,String dataType) {
        String indexGroup = datagridVO.getIndexGroup();
        String yearMonth = datagridVO.getIndexYearMonth();
        if(com.baomidou.mybatisplus.core.toolkit.StringUtils.isEmpty(yearMonth)) {
            yearMonth = EsDateUtils.getIndexYearMonth(new Date());
        }
        Integer pageNo = datagridVO.getPageNo();
        if(pageNo == null){
            return Result.error("参数缺失");
        }
        Integer pageSize = datagridVO.getPageSize();
        if(pageSize == null){
            return Result.error("参数缺失");
        }
        String searchYear = yearMonth.substring(0,4);
//        if(checkYear(searchYear)){
//            return Result.ok("该年份数据不存在");
//        }
        Map<String,Object> param = new HashMap<>();

        if(indexGroup != null){
            String orgGroup = OrgGroupUtil.changeToCG(indexGroup);
            param.put("orgGroup",orgGroup);
        }
        param.put("indexValueEdit",datagridVO.getIndexValueEdit());
        if(datagridVO.getIndexCodes() != null && datagridVO.getIndexCodes().size() > 0){
            param.put("indexCodes",datagridVO.getIndexCodes());
        }
        if(datagridVO.getIndexTypes() != null && datagridVO.getIndexTypes().size() > 0){
            param.put("indexTypes",datagridVO.getIndexTypes());
        }
        if(datagridVO.getIndexAttrs() != null && datagridVO.getIndexAttrs().size() > 0){
            List<String> attrs = datagridVO.getIndexAttrs();
            List<String> attrsFinal = new ArrayList<>();
            for(String attr : attrs){
                attrsFinal.add(attr);
                if(attr.equals(InfoAttrUtil.SHI_JI_ZHI)){
                    attrsFinal.add(InfoAttrUtil.SHU_LIANG);
                }
                if(attr.equals(InfoAttrUtil.DE_FEN)){
                    attrsFinal.add(InfoAttrUtil.XIAOXIANG_DE_FEN);
                    attrsFinal.add(InfoAttrUtil.SHUANG_XIANG_PING_FEN);
                }
            }
            param.put("indexAttrs",attrsFinal);
        }
        List<Map<String,Object>> result = new ArrayList<>();
        if(pageNo == 1){
            param.put("zongji",InfoAttrUtil.ZONG_JI);
            result = dataGridMapper.getIndexInfoMapByParamater(param);
        }
        if(pageNo == 1){
            param.put("min", (pageNo - 1) * pageSize);
            param.put("max", pageSize-1);
        }else{
            param.put("min", (pageNo - 1) * pageSize-1);
            param.put("max", pageSize);
        }
        param.put("zongji",null);
        //根据indexCode 和 indexValueCode排序
        List<Map<String,Object>> tempResult = dataGridMapper.getIndexInfoMapByParamater(param);
        result.addAll(tempResult);
        int count = dataGridMapper.getIndexInfoCountMapByParamater(param);
        dynamicColumnData(result,indexGroup,yearMonth,noVirtual,dataType);
        Page<Map<String,Object>> finalPage = new Page<Map<String,Object>>(pageNo, pageSize);
        //如或是最后一页，且考核期长度为8位，则展示调整记录
        if(yearMonth.length() == 8 && ((pageNo-1)*pageSize+result.size()) == count && result.size() < pageSize){
            dynamicColumnDataForChangeReport(result,indexGroup,yearMonth,noVirtual,dataType);
        }
        if(yearMonth.length() == 8){
            count = count+1;
        }

        finalPage.setTotal(count);
        finalPage.setCurrent(pageNo);
        finalPage.setSize(pageSize);
        int pages = (count) / pageSize;
        if ((count) % pageSize > 0) {
            pages += 1;
        }
        finalPage.setPages(pages);
        finalPage.setRecords(result);
        return Result.ok(finalPage);
    }

    /**
     * 审核员绩效考核详情/绩效管理员绩效审核详情-列表信息
     * @return
     */
    @Override
    public Result<?> auditDetailDatagrid(DatagridVO datagridVO,String noVirtual,String dataType) {
        String indexGroup = datagridVO.getIndexGroup();
        String yearMonth = datagridVO.getIndexYearMonth();
        if(com.baomidou.mybatisplus.core.toolkit.StringUtils.isEmpty(yearMonth)) {
            yearMonth = EsDateUtils.getIndexYearMonth(new Date());
        }
        Integer pageNo = datagridVO.getPageNo();
        if(pageNo == null){
            return Result.error("参数缺失");
        }
        Integer pageSize = datagridVO.getPageSize();
        if(pageSize == null){
            return Result.error("参数缺失");
        }
        String searchYear = yearMonth.substring(0,4);
//        if(checkYear(searchYear)){
//            return Result.ok("该年份数据不存在");
//        }
        Map<String,Object> param = new HashMap<>();

        if(indexGroup != null){
            String orgGroup = OrgGroupUtil.changeToCG(indexGroup);
            param.put("orgGroup",orgGroup);
        }
        param.put("indexValueEdit",datagridVO.getIndexValueEdit());
        if(datagridVO.getIndexCodes() != null && datagridVO.getIndexCodes().size() > 0){
            param.put("indexCodes",datagridVO.getIndexCodes());
        }
        if(datagridVO.getIndexTypes() != null && datagridVO.getIndexTypes().size() > 0){
            param.put("indexTypes",datagridVO.getIndexTypes());
        }
        if(datagridVO.getIndexAttrs() != null && datagridVO.getIndexAttrs().size() > 0){
            List<String> attrs = datagridVO.getIndexAttrs();
            List<String> attrsFinal = new ArrayList<>();
            for(String attr : attrs){
                attrsFinal.add(attr);
                if(attr.equals(InfoAttrUtil.SHI_JI_ZHI)){
                    attrsFinal.add(InfoAttrUtil.SHU_LIANG);
                }
                if(attr.equals(InfoAttrUtil.DE_FEN)){
                    attrsFinal.add(InfoAttrUtil.XIAOXIANG_DE_FEN);
                    attrsFinal.add(InfoAttrUtil.SHUANG_XIANG_PING_FEN);
                }
            }
            param.put("indexAttrs",attrsFinal);
        }
        //是否展示简报
        String isTotal = datagridVO.getIsTotal();
        List<Map<String,Object>> result = new ArrayList<>();
        param.put("isTotal",isTotal);
        if(pageNo == 1){
            param.put("zongji",InfoAttrUtil.ZONG_JI);
            result = dataGridMapper.getIndexInfoMapByParamater(param);
        }
        if(pageNo == 1){
            param.put("min", (pageNo - 1) * pageSize);
            param.put("max", pageSize-1);
        }else{
            param.put("min", (pageNo - 1) * pageSize-1);
            param.put("max", pageSize);
        }
        param.put("zongji",null);
        List<Map<String,Object>> tempResult = dataGridMapper.getIndexInfoMapByParamater(param);
        result.addAll(tempResult);
        if(isTotal.equals("true")){
            for(Map<String,Object> map1:result){
                map1.put("showLine",false);
            }

        }else {
            for(Map<String,Object> map1:result){
                map1.put("showLine",true);
            }
        }
        int count = dataGridMapper.getIndexInfoCountMapByParamater(param);
        kaFillDatagridDynamicColumnData(result,indexGroup,yearMonth,noVirtual,dataType);
        Page<Map<String,Object>> finalPage = new Page<Map<String,Object>>(pageNo, pageSize);
        //如或是最后一页，且考核期长度为8位，则展示调整记录
        if(yearMonth.length() == 8 && ((pageNo-1)*pageSize+result.size()) == count && result.size() < pageSize){
            dynamicColumnDataForChangeReport(result,indexGroup,yearMonth,noVirtual,dataType);
        }
        if(yearMonth.length() == 8){
            count = count+1;
        }

        finalPage.setTotal(count);
        finalPage.setCurrent(pageNo);
        finalPage.setSize(pageSize);
        int pages = (count) / pageSize;
        if ((count) % pageSize > 0) {
            pages += 1;
        }
        finalPage.setPages(pages);
        finalPage.setRecords(result);
        return Result.ok(finalPage);
    }

    /**
     * 动态列展示各部门调整记录
     * @param result
     * @param indexGroup
     * @param yearMonth
     * @return
     */
    public List<Map<String, Object>> dynamicColumnDataForChangeReport(List<Map<String,Object>> result,String indexGroup,String yearMonth,String noVirtual,String dataType){
        Map<String,Object> changReportResultMap = new HashMap<>();
        Map<String,Object> orgParam = new HashMap<>();
        orgParam.put("noVirtual",noVirtual);
        orgParam.put("orgGroup",indexGroup);
        List<Map<String, Object>> orgs = dataGridMapper.getOrgAndUserByOrgGroup(orgParam);
        Map<String,Object> dataGridParam = new HashMap<>();
        String year = yearMonth.substring(0, 4);
        String month = yearMonth.substring(6);
        String currentYearMonth = EsDateUtils.getIndexYearMonth(new Date());
        String currentTotalYearMonth = currentYearMonth.substring(0,4)+"01"+currentYearMonth.substring(4);
        //非当前考核数据
        if(!currentYearMonth.equals(yearMonth) && !currentTotalYearMonth.equals(yearMonth)){
            List<Map<String, Object>> newOrgs = new ArrayList<>();
            departUserChangeService.changeUserForCondition(yearMonth,orgs,newOrgs);
            orgs = newOrgs;
        }
        changReportResultMap.put("indexName","调整记录");

        for (Map<String,Object> org : orgs ) {
            String orgColumn = org.get("code").toString();
            String indexDepart = orgColumn.substring(0,12);
            dataGridParam.put("indexDepart",indexDepart);
            if(orgColumn.length() > 12){
                dataGridParam.put("indexScorer",orgColumn.substring(12));
            }else{
                dataGridParam.put("indexScorer",null);
            }
            dataGridParam.put("yearMonth",year+month);
            dataGridParam.put("dataType",dataType);
            Map<String, Object> changReport = new HashMap<>();
            changReport = dataGridMapper.getChangeReportForParam(dataGridParam);
            if(changReport != null){
                changReportResultMap.put(org.get("code").toString(),changReport.get("comments"));

            }else{
                changReportResultMap.put(org.get("code").toString(),null);
            }

        }
        result.add(changReportResultMap);
        return result;
    }

    /**
     * 动态列根据指标查询各个部门对应月份指标评分数据
     * @param result
     * @param indexGroup
     * @param yearMonth
     * @return
     */
    public List<Map<String, Object>> dynamicColumnData(List<Map<String,Object>> result,String indexGroup,String yearMonth,String noVirtual,String dataType){
        Map<String,Object> orgParam = new HashMap<>();
        orgParam.put("noVirtual",noVirtual);
        orgParam.put("orgGroup",indexGroup);
        List<Map<String, Object>> orgs = dataGridMapper.getOrgAndUserByOrgGroup(orgParam);
        Map<String,Object> dataGridParam = new HashMap<>();
        String year = yearMonth.substring(0, 4);
        String month ="";
        if(yearMonth.length() == 6){
            month = yearMonth.substring(4);
        }else{
            month = yearMonth.substring(6);
        }
        String currentYearMonth = EsDateUtils.getIndexYearMonth(new Date());
        String currentTotalYearMonth = currentYearMonth.substring(0,4)+"01"+currentYearMonth.substring(4);
        //非当前考核数据
        if(!currentYearMonth.equals(yearMonth) && !currentTotalYearMonth.equals(yearMonth)){
            List<Map<String, Object>> newOrgs = new ArrayList<>();
            departUserChangeService.changeUserForCondition(yearMonth,orgs,newOrgs);
            orgs = newOrgs;
        }
        String[] roleCodes = {"ykgm","ykkpiowner"};
        List<String> userNames = sysUserMapper.selectUserNameByRoleCodes(roleCodes);
        userNames.add("system");
        for ( int i = 0 ; i < result.size() ; i++ ){
            for (Map<String,Object> org : orgs ) {
                String orgColumn = org.get("code").toString();
                String indexDepart = orgColumn.substring(0,12);
                dataGridParam.put("indexDepart",indexDepart);
                if(orgColumn.length() > 12){
                    dataGridParam.put("indexScorer",orgColumn.substring(12));
                }else{
                    dataGridParam.put("indexScorer",null);
                }
                dataGridParam.put("indexExecutors",userNames);
                dataGridParam.put("year",year);
                dataGridParam.put("indexValueCode",result.get(i).get("indexValueCode").toString());
                dataGridParam.put("dataType",dataType);
                Map<String, Object> monthValue = new HashMap<>();
                if(yearMonth.length() == 6){
                    dataGridParam.put("month","m"+month);
                    if(EsDateUtils.getIndexYearMonth(new Date()).equals(yearMonth)){
                        monthValue = dataGridMapper.getMonthIndexValueForParam(dataGridParam);
                    }else{
                        dataGridParam.put("mirrorCode","MC"+yearMonth);
                        monthValue = dataGridMapper.getMirrorMonthIndexValueForParam(dataGridParam);
                    }
                    if(monthValue != null){
                        if(result.get(i).get("indexFormate").toString().equals("3")){
                            String radio = DepartCode.formateForDoubleScale1(DoubleUtil.mul(Double.valueOf(monthValue.get("m"+yearMonth.substring(4)).toString()),100d))+"%";
                            result.get(i).put(org.get("code").toString(),radio);

                        }else{
                            result.get(i).put(org.get("code").toString(),DepartCode.formateForDouble(Double.valueOf(monthValue.get("m"+yearMonth.substring(4)).toString())));
                        }
                    }else{
                        result.get(i).put(org.get("code").toString(),null);
                    }
                }else{
                    String mirrorCode = "MC"+year+yearMonth.substring(6);
                    dataGridParam.put("month","m_total");
                    if(EsDateUtils.getIndexYearMonth(new Date()).substring(4).equals(yearMonth.substring(6))){
                        monthValue = dataGridMapper.getMonthIndexValueForParam(dataGridParam);
                    }else{
                        dataGridParam.put("mirrorCode",mirrorCode);
                        monthValue = dataGridMapper.getMirrorMonthIndexValueForParam(dataGridParam);
                    }
                    if(monthValue != null){
                        if(result.get(i).get("indexFormate").toString().equals("3")){
                            String radio = DepartCode.formateForDoubleScale1(DoubleUtil.mul(Double.valueOf(monthValue.get("m_total").toString()),100d))+"%";
                            result.get(i).put(org.get("code").toString(),radio);

                        }else{
                            result.get(i).put(org.get("code").toString(),DepartCode.formateForDouble(Double.valueOf(monthValue.get("m_total").toString())));
                        }
                    }else{
                        result.get(i).put(org.get("code").toString(),null);
                    }
                }

            }
        }
        return result;
    }


    /**
     * 动态列根据指标查询各个部门对应月份指标评分数据(目标值实际值填报/目标值填报/审核员绩效考核详情/绩效管理员绩效审核详情-列表信息)
     * @param result
     * @param indexGroup
     * @param yearMonth
     * @return
     */
    public List<Map<String, Object>> kaFillDatagridDynamicColumnData(List<Map<String,Object>> result,String indexGroup,String yearMonth,String noVirtual,String dataType){
        Map<String,Object> orgParam = new HashMap<>();
        orgParam.put("noVirtual",noVirtual);
        orgParam.put("orgGroup",indexGroup);
        List<Map<String, Object>> orgs = dataGridMapper.getOrgAndUserByOrgGroup(orgParam);
        Map<String,Object> dataGridParam = new HashMap<>();
        String year = yearMonth.substring(0, 4);
        String month = yearMonth.substring(4);
        String[] roleCodes = {"ykgm","ykkpiowner"};
        List<String> userNames = sysUserMapper.selectUserNameByRoleCodes(roleCodes);
        userNames.add("system");
        for ( int i = 0 ; i < result.size() ; i++ ){
            for (Map<String,Object> org : orgs ) {
                String orgColumn = org.get("code").toString();
                String indexDepart = orgColumn.substring(0,12);
                dataGridParam.put("indexDepart",indexDepart);
                if(orgColumn.length() > 12){
                    dataGridParam.put("indexScorer",orgColumn.substring(12));
                }else{
                    dataGridParam.put("indexScorer",null);
                }
                dataGridParam.put("indexExecutors",userNames);
                dataGridParam.put("year",year);
                dataGridParam.put("indexValueCode",result.get(i).get("indexValueCode").toString());
                dataGridParam.put("dataType",dataType);
                Map<String, Object> monthValue = new HashMap<>();
                if(yearMonth.length() == 6){
                    dataGridParam.put("month","m"+month);
                    monthValue = dataGridMapper.getMonthIndexValueForParam(dataGridParam);
                    if(monthValue != null){
                        if(result.get(i).get("indexFormate").toString().equals("3")){
                            String radio = DepartCode.formateForDoubleScale1(DoubleUtil.mul(Double.valueOf(monthValue.get("m"+yearMonth.substring(4)).toString()),100d))+"%";
                            result.get(i).put(org.get("code").toString(),radio);

                        }else{
                            result.get(i).put(org.get("code").toString(),DepartCode.formateForDouble(Double.valueOf(monthValue.get("m"+yearMonth.substring(4)).toString())));
                        }
                    }else{
                        result.get(i).put(org.get("code").toString(),null);
                    }
                }else{
                    dataGridParam.put("month","m_total");
                    if(EsDateUtils.getIndexYearMonth(new Date()).substring(4).equals(yearMonth.substring(6))){
                        monthValue = dataGridMapper.getMonthIndexValueForParam(dataGridParam);
                    }else{
                        String mirrorCode = "MC"+year+yearMonth.substring(6);
                        dataGridParam.put("mirrorCode",mirrorCode);
                        monthValue = dataGridMapper.getMirrorMonthIndexValueForParam(dataGridParam);
                    }
                    if(monthValue != null){
                        if(result.get(i).get("indexFormate").toString().equals("3")){
                            String radio = DepartCode.formateForDoubleScale1(DoubleUtil.mul(Double.valueOf(monthValue.get("m_total").toString()),100d))+"%";
                            result.get(i).put(org.get("code").toString(),radio);

                        }else{
                            result.get(i).put(org.get("code").toString(),DepartCode.formateForDouble(Double.valueOf(monthValue.get("m_total").toString())));
                        }
                    }else{
                        result.get(i).put(org.get("code").toString(),null);
                    }
                }

            }
        }
        return result;
    }

    /**
     * 目标值列表/绩效填报列表保存
     * @param datagridVO
     * @return
     */
    @Override
    public Result<?> indexValueDataSave(DatagridVO datagridVO,String dataType) {
        List<Map<String,Object>> values = datagridVO.getValues();
        String yearMonth = datagridVO.getIndexYearMonth();
        if(StringUtils.isEmpty(yearMonth)){
            return Result.error("参数缺失");
        }
        String indexGroup = datagridVO.getIndexGroup();
        if(StringUtils.isEmpty(indexGroup)){
            return Result.error("参数缺失");
        }
        String currentYearMonth = EsDateUtils.getIndexYearMonth(new Date());
        if(currentYearMonth.equals(yearMonth) || (currentYearMonth.substring(0,4)+"01"+currentYearMonth.substring(4)).equals(yearMonth)){
            QueryWrapper<TkIndexValueAudit> queryWrapperSdf = new QueryWrapper<>();
            queryWrapperSdf.lambda().eq(TkIndexValueAudit::getIndexGroup, "C".equals(indexGroup) ? "A01A01A01" : "A01A01A02");
            queryWrapperSdf.lambda().eq(TkIndexValueAudit::getIndexValueY, yearMonth.substring(0,4));
            queryWrapperSdf.lambda().eq(TkIndexValueAudit::getIndexValueM, yearMonth.substring(4));
            queryWrapperSdf.lambda().eq(TkIndexValueAudit::getDataType,dataType);
            TkIndexValueAudit indexValueAuditEntity = tkIndexValueAuditMapper.selectOne(queryWrapperSdf);
            if (indexValueAuditEntity != null) {
                if("2".equals(indexValueAuditEntity.getAuditStatus())){
                    return Result.error("部门指标正在审核中，不允许修改");
                }else if("3".equals(indexValueAuditEntity.getAuditStatus())){
                    return Result.error("部门指标已审核，不允许修改");
                }

            }
        }else{
            return Result.error("镜像数据，不允许修改");
        }

        String year = yearMonth.substring(0,4);
        String month ="";
        if(yearMonth.length() == 6){
            month = yearMonth.substring(4);
        }else{
            month = yearMonth.substring(6);
        }

        if(values == null || values.size() == 0 ){
            return Result.ok(null);
        }
        Map<String,Object> param = new HashMap<>();
        param.put("orgGroup",indexGroup);
        List<String> orgCodes = dataGridMapper.getOrgCodeByOrgGroup(param);
        Map<String,Object> dataGridParam = new HashMap<>();
        dataGridParam.put("year",year);
        for(Map<String,Object> rowData : datagridVO.getValues()){
            Iterator it = rowData.keySet().iterator();
            while (it.hasNext()) {
                String columData = it.next().toString();
                if(columData.length() >= 12){
                    if(orgCodes.contains(columData.substring(0,12))){
                        dataGridParam.put("indexDepart",columData.substring(0,12));
                        if(columData.length() > 12){
                            dataGridParam.put("indexScorer",columData.substring(12));
                        }else{
                            dataGridParam.put("indexScorer",null);
                        }
                        if(rowData.get("indexCode") != null){
                            dataGridParam.put("indexValueCode",rowData.get("indexValueCode").toString());
                            if(yearMonth.length() == 6){
                                dataGridParam.put("month","m"+month);
                            }else{
                                dataGridParam.put("month","m_total");
                            }
                            dataGridParam.put("dataType",dataType);
                            String[] roleCodes = {"ykgm"};
                            List<String> userNames = sysUserMapper.selectUserNameByRoleCodes(roleCodes);
                            userNames.add("system");
                            dataGridParam.put("indexExecutors",userNames);
                            Map<String, Object> monthValue = dataGridMapper.getMonthIndexValueForParam(dataGridParam);
                            if(monthValue != null){
                                double grade = TkGradeUtil.changeFormate(rowData.get(columData).toString());
                                monthValue.put("indexValueStatus",month.startsWith("0")? Integer.valueOf(month.substring(1)):Integer.valueOf(month));
                                List<TkIndexValueAdjust> adjusts = new ArrayList<>();
                                TkIndexValue oldIndexValue = JSON.parseObject(JSON.toJSONString(monthValue), TkIndexValue.class);
                                oldIndexValue.setIndexDepart(dataGridParam.get("indexDepart").toString());
                                oldIndexValue.setIndexValueCode(rowData.get("indexValueCode").toString());
                                oldIndexValue.setIndexScorer(monthValue.get("indexScorer").toString());
                                if(yearMonth.length() == 6){
                                    monthValue.put("m"+month,grade);
                                }else{
                                    monthValue.put("mTotal",grade);
                                }

                                TkIndexValue tkIndexValueEntity = JSON.parseObject(JSON.toJSONString(monthValue), TkIndexValue.class);
                                tkIndexValueEntity.setDataType(dataType);
                                List<TkIndexValue> list = new ArrayList<>();
                                Map<String,Object> attrParam = new HashMap<>();
                                attrParam.put("id",tkIndexValueEntity.getId());
                                String attrValue = tkGmMapper.selectAttrValueByParam(attrParam);
                                //目标值只根据dataType去修改，其他指标同时修改
                                if(StringUtils.isNotEmpty(attrValue) && attrValue.equals(InfoAttrUtil.MU_BIAO_ZHI)){
                                    list.add(tkIndexValueEntity);
                                }else{
                                    List<TkIndexValue> oldTkIndexValues = tkGmMapper.selectAllDataTypeIndexValueByParam(attrParam);
                                    for(TkIndexValue oldTkIndexValue : oldTkIndexValues){
                                        TkIndexValue saveTkIndexValue = new TkIndexValue();
                                        BeanUtils.copyProperties(tkIndexValueEntity,saveTkIndexValue);
                                        saveTkIndexValue.setId(oldTkIndexValue.getId());
                                        saveTkIndexValue.setDataType(oldTkIndexValue.getDataType());
                                        list.add(saveTkIndexValue);
                                    }
                                }
                                for(TkIndexValue tkIndexValue : list){
                                    TkGradeUtil.makeChangeAdjustData(oldIndexValue,tkIndexValueEntity,adjusts,year,tkIndexValue.getDataType());
                                    for(int i = 0 ; i < adjusts.size() ; i++){
                                        QueryWrapper<TkIndexValueAdjust> adjustQuery = new QueryWrapper<>();
                                        adjustQuery.lambda().eq(TkIndexValueAdjust::getIndexValueCode,adjusts.get(i).getIndexValueCode());
                                        adjustQuery.lambda().eq(TkIndexValueAdjust::getIndexDepart,adjusts.get(i).getIndexDepart());
                                        adjustQuery.lambda().eq(TkIndexValueAdjust::getYear,year);
                                        adjustQuery.lambda().eq(TkIndexValueAdjust::getWhich,adjusts.get(i).getWhich());
                                        if(StringUtils.isNotEmpty(adjusts.get(i).getDataType())){
                                            adjustQuery.lambda().eq(TkIndexValueAdjust::getDataType,adjusts.get(i).getDataType());
                                        }
                                        List<TkIndexValueAdjust> oldAdjusts = tkIndexValueAdjustMapper.selectList(adjustQuery);
                                        if(oldAdjusts.size() == 0){
                                            tkIndexValueAdjustMapper.insert(adjusts.get(i));
                                        }else{
                                            for(TkIndexValueAdjust oldAdjust : oldAdjusts){
                                                adjusts.get(i).setId(oldAdjust.getId());
                                                tkIndexValueAdjustMapper.updateById(adjusts.get(i));
                                            }

                                        }
                                    }
                                    tkIndexValueMapper.updateById(tkIndexValue);
                                    log.info("udpate index value entity:\r\n{}", tkIndexValue);
                                }
                            }
                        }else{
                            deleteAndSaveChangeReport(dataGridParam.get("indexScorer").toString(),dataGridParam.get("indexDepart").toString(),dataType,yearMonth,(String)rowData.get(columData));
                        }
                    }
                }
            }
        }
        return Result.ok("操作成功");
    }

    public void deleteAndSaveChangeReport(String indexScorer,String indexDepart,String dataType,String yearMonth,String comments){
        QueryWrapper<TkChangeReport> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(TkChangeReport::getIndexDepart,indexDepart);
        queryWrapper.lambda().eq(TkChangeReport::getIndexScorer,indexScorer);
        queryWrapper.lambda().eq(TkChangeReport::getDataType,dataType);
        if(yearMonth.length() == 8){
            yearMonth = yearMonth.substring(0,4)+yearMonth.substring(6);
        }
        queryWrapper.lambda().eq(TkChangeReport::getIndexYearMonth,yearMonth);
        tkChangeReportMapper.delete(queryWrapper);
        if(StringUtils.isNotEmpty(comments)){
            TkChangeReport tkChangeReport = new TkChangeReport();
            tkChangeReport.setIndexDepart(indexDepart);
            tkChangeReport.setIndexScorer(indexScorer);
            tkChangeReport.setIndexYearMonth(yearMonth);
            tkChangeReport.setDataType(dataType);
            tkChangeReport.setComments(comments);
            tkChangeReportMapper.insert(tkChangeReport);
        }

    }

    /**
     * 目标值实际值填报列表保存
     * @param datagridVO
     * @return
     */
    @Override
    public Result<?> kaFillSave(DatagridVO datagridVO,String dataType) {
        List<Map<String,Object>> values = datagridVO.getValues();
        String yearMonth = datagridVO.getIndexYearMonth();
        if(StringUtils.isEmpty(yearMonth)){
            return Result.error("参数缺失");
        }
        String indexGroup = datagridVO.getIndexGroup();
        if(StringUtils.isEmpty(indexGroup)){
            return Result.error("参数缺失");
        }
        String currentYearMonth = EsDateUtils.getIndexYearMonth(new Date());
        if(yearMonth.length() == 6 || (yearMonth.length() == 8 && (currentYearMonth.substring(0,4)+"01"+currentYearMonth.substring(4)).equals(yearMonth))){
            QueryWrapper<TkIndexValueAudit> queryWrapperSdf = new QueryWrapper<>();
            queryWrapperSdf.lambda().eq(TkIndexValueAudit::getIndexGroup, "C".equals(indexGroup) ? "A01A01A01" : "A01A01A02");
            queryWrapperSdf.lambda().eq(TkIndexValueAudit::getIndexValueY, currentYearMonth.substring(0,4));
            queryWrapperSdf.lambda().eq(TkIndexValueAudit::getIndexValueM, currentYearMonth.substring(4));
            queryWrapperSdf.lambda().eq(TkIndexValueAudit::getDataType,dataType);
            TkIndexValueAudit indexValueAuditEntity = tkIndexValueAuditMapper.selectOne(queryWrapperSdf);
            if (indexValueAuditEntity != null) {
                if("2".equals(indexValueAuditEntity.getAuditStatus())){
                    return Result.error("部门指标正在审核中，不允许修改");
                }else if("3".equals(indexValueAuditEntity.getAuditStatus())){
                    return Result.error("部门指标已审核，不允许修改");
                }

            }
        }else{
            return Result.error("镜像数据，不允许修改");
        }

        String year = yearMonth.substring(0,4);
        String month = yearMonth.substring(4);
        if(values == null || values.size() == 0 ){
            return Result.ok(null);
        }
        Map<String,Object> param = new HashMap<>();
        param.put("orgGroup",indexGroup);
        List<String> orgCodes = dataGridMapper.getOrgCodeByOrgGroup(param);
        Map<String,Object> dataGridParam = new HashMap<>();
        dataGridParam.put("year",year);
        for(Map<String,Object> rowData : datagridVO.getValues()){
            dataGridParam.put("indexValueCode",rowData.get("indexValueCode").toString());
            Iterator it = rowData.keySet().iterator();
            while (it.hasNext()) {
                String columData = it.next().toString();
                if(columData.length() >= 12){
                    if(orgCodes.contains(columData.substring(0,12))){
                        dataGridParam.put("indexDepart",columData.substring(0,12));
                        if(columData.length() > 12){
                            dataGridParam.put("indexScorer",columData.substring(12));
                        }else{
                            dataGridParam.put("indexScorer",null);
                        }
                        if(yearMonth.length() == 6){
                            dataGridParam.put("month","m"+month);
                        }else{
                            dataGridParam.put("month","m_total");
                        }
                        dataGridParam.put("dataType",dataType);
                        String[] roleCodes = {"ykgm"};
                        List<String> userNames = sysUserMapper.selectUserNameByRoleCodes(roleCodes);
                        userNames.add("system");
                        dataGridParam.put("indexExecutors",userNames);
                        Map<String, Object> monthValue = dataGridMapper.getMonthIndexValueForParam(dataGridParam);
                        if(monthValue != null){
                            double grade = TkGradeUtil.changeFormate(rowData.get(columData).toString());
                            monthValue.put("indexValueStatus",month.startsWith("0")? Integer.valueOf(month.substring(1)):Integer.valueOf(month));
                            List<TkIndexValueAdjust> adjusts = new ArrayList<>();
                            TkIndexValue oldIndexValue = JSON.parseObject(JSON.toJSONString(monthValue), TkIndexValue.class);
                            oldIndexValue.setIndexDepart(dataGridParam.get("indexDepart").toString());
                            oldIndexValue.setIndexValueCode(rowData.get("indexValueCode").toString());
                            oldIndexValue.setIndexScorer(monthValue.get("indexScorer").toString());
                            if(yearMonth.length() == 6){
                                monthValue.put("m"+month,grade);
                            }else{
                                monthValue.put("mTotal",grade);
                            }
                            TkIndexValue tkIndexValueEntity = JSON.parseObject(JSON.toJSONString(monthValue), TkIndexValue.class);
                            tkIndexValueEntity.setDataType(dataType);
                            List<TkIndexValue> list = new ArrayList<>();
                            Map<String,Object> attrParam = new HashMap<>();
                            attrParam.put("id",tkIndexValueEntity.getId());
                            String attrValue = tkGmMapper.selectAttrValueByParam(attrParam);
                            //目标值只根据dataType去修改，其他指标同时修改
                            if(StringUtils.isNotEmpty(attrValue) && attrValue.equals(InfoAttrUtil.MU_BIAO_ZHI)){
                                list.add(tkIndexValueEntity);
                            }else{
                                List<TkIndexValue> oldTkIndexValues = tkGmMapper.selectAllDataTypeIndexValueByParam(attrParam);
                                for(TkIndexValue oldTkIndexValue : oldTkIndexValues){
                                    TkIndexValue saveTkIndexValue = new TkIndexValue();
                                    BeanUtils.copyProperties(tkIndexValueEntity,saveTkIndexValue);
                                    saveTkIndexValue.setId(oldTkIndexValue.getId());
                                    saveTkIndexValue.setDataType(oldTkIndexValue.getDataType());
                                    list.add(saveTkIndexValue);
                                }
                            }
                            for(TkIndexValue tkIndexValue : list){
                                TkGradeUtil.makeChangeAdjustData(oldIndexValue,tkIndexValueEntity,adjusts,year,tkIndexValue.getDataType());
                                for(int i = 0 ; i < adjusts.size() ; i++){
                                    QueryWrapper<TkIndexValueAdjust> adjustQuery = new QueryWrapper<>();
                                    adjustQuery.lambda().eq(TkIndexValueAdjust::getIndexValueCode,adjusts.get(i).getIndexValueCode());
                                    adjustQuery.lambda().eq(TkIndexValueAdjust::getIndexDepart,adjusts.get(i).getIndexDepart());
                                    adjustQuery.lambda().eq(TkIndexValueAdjust::getYear,year);
                                    adjustQuery.lambda().eq(TkIndexValueAdjust::getWhich,adjusts.get(i).getWhich());
                                    if(StringUtils.isNotEmpty(adjusts.get(i).getDataType())){
                                        adjustQuery.lambda().eq(TkIndexValueAdjust::getDataType,adjusts.get(i).getDataType());
                                    }
                                    List<TkIndexValueAdjust> oldAdjusts = tkIndexValueAdjustMapper.selectList(adjustQuery);
                                    if(oldAdjusts.size() == 0){
                                        tkIndexValueAdjustMapper.insert(adjusts.get(i));
                                    }else{
                                        for(TkIndexValueAdjust oldAdjust : oldAdjusts){
                                            adjusts.get(i).setId(oldAdjust.getId());
                                            tkIndexValueAdjustMapper.updateById(adjusts.get(i));
                                        }

                                    }
                                }
                                tkIndexValueMapper.updateById(tkIndexValue);
                                log.info("udpate index value entity:\r\n{}", tkIndexValue);
                            }
                        }
                    }
                }
            }
        }
        return Result.ok("操作成功");
    }

    /**
     *
     *权重管理-列表信息
     * @param datagridVO
     * @param noVirtual
     * @return
     */
    @Override
    public Result<?> kaWeightDatagrid(DatagridVO datagridVO, String noVirtual,String dataType) {
        String indexGroup = datagridVO.getIndexGroup();
        String yearMonth = datagridVO.getIndexYearMonth();
        if(StringUtils.isEmpty(yearMonth)){
            return Result.error("参数缺失");
        }
        Integer pageNo = datagridVO.getPageNo();
        if(pageNo == null){
            return Result.error("参数缺失");
        }
        Integer pageSize = datagridVO.getPageSize();
        if(pageSize == null){
            return Result.error("参数缺失");
        }
        String searchYear = yearMonth.substring(0,4);
//        if(checkYear(searchYear)){
//            return Result.ok("该年份数据不存在");
//        }
        Map<String,Object> param = new HashMap<>();
        param.put("min", (pageNo - 1) * pageSize);
        param.put("max", pageSize);
        if(indexGroup != null){
            String orgGroup = OrgGroupUtil.changeToCG(indexGroup);
            param.put("indexGroup",orgGroup);
        }
        List<String> infoAttrs = new ArrayList<>();
        infoAttrs.add(InfoAttrUtil.XIAO_JI);
        infoAttrs.add(InfoAttrUtil.ZONG_JI);
        List<String> noContainCodes = departAndInfoTool.getIndexCodesByOrgGroup(indexGroup,infoAttrs);
        param.put("noContainCodes",noContainCodes);
        param.put("indexCodes",datagridVO.getIndexCodes());
        List<Map<String,Object>> result = commonMapper.getIndexCodeByParam(param);
        int count = commonMapper.getIndexCodeCountByOrgGroup(param);
        Map<String,Object> orgParam = new HashMap<>();
        orgParam.put("noVirtual",noVirtual);
        orgParam.put("orgGroup",indexGroup);
        List<Map<String, Object>> orgs = dataGridMapper.getOrgByOrgGroup(orgParam);
        Map<String,Object> dataGridParam = new HashMap<>();
        for ( int i = 0 ; i < result.size() ; i++ ){
            result.get(i).put("indexName",result.get(i).get("name"));
            result.get(i).put("indexCode",result.get(i).get("code"));
            for (Map<String,Object> org : orgs ) {
                dataGridParam.put("orgCode",org.get("code").toString());
                dataGridParam.put("year",yearMonth.substring(0,4));
                dataGridParam.put("indexCode",result.get(i).get("indexCode").toString());
                dataGridParam.put("month","WEIGHT_M"+yearMonth.substring(4));
                Map<String, Object> monthValue = dataGridMapper.getMonthIndexWeightForParam(dataGridParam);
                result.get(i).put(org.get("code").toString(),monthValue != null ? monthValue.get("WEIGHT_M"+yearMonth.substring(4)).toString() : null);
                result.get(i).put("indexType",monthValue != null ? monthValue.get("indexType").toString() : null);
                result.get(i).put("indexTypeName",monthValue != null ? monthValue.get("indexTypeName").toString() : null);
            }
        }
        Page<Map<String,Object>> finalPage = new Page<Map<String,Object>>(pageNo, pageSize);
        finalPage.setTotal(count);
        finalPage.setCurrent(pageNo);
        finalPage.setSize(pageSize);
        int pages = count / pageSize;
        if (count % pageSize > 0) {
            pages += 1;
        }
        finalPage.setPages(pages);
        finalPage.setRecords(result);

        PmsWeightPage<Map<String,Object>> PmsWeightPage = new PmsWeightPage<Map<String,Object>>(finalPage);
        //加上部门一个所有指标的重权之和
        Map<String,Object> weightTotalMap = new HashMap<>();
        for (Map<String,Object> org : orgs ) {
            Map<String,Object> paramWeight = new HashMap<>();
            paramWeight.put("orgCode",org.get("code").toString());
            paramWeight.put("month","WEIGHT_M"+yearMonth.substring(4));
            Double weightTotal = dataGridMapper.selectOrgWeightSum(paramWeight);
            weightTotalMap.put(org.get("code").toString(),weightTotal+"%");
        }

        PmsWeightPage.setWeightTotal(weightTotalMap);
        return Result.ok(PmsWeightPage);
    }

    public String makeWeightDataFormate(String data,String indexType){
        if("1".equals(indexType)){
            data = data.substring(0,data.length()-1);
        }else if("2".equals(indexType)){
            data = data.substring(1,data.length()-1);
        }else{

        }
        return data;
    }

    /**
     * 权重管理-列表保存
     * @param datagridVO
     * @return
     */
    @Override
    public Result<?> kaWeightSave(DatagridVO datagridVO,String dataType) {
        List<Map<String,Object>> values = datagridVO.getValues();
        String yearMonth = datagridVO.getIndexYearMonth();
        String indexGroup = datagridVO.getIndexGroup();
        String year = yearMonth.substring(0,4);
        String month = yearMonth.substring(4);
        if(values == null || values.size() == 0 ){
            return Result.ok(null);
        }
        Map<String,Object> param = new HashMap<>();
        param.put("orgGroup",indexGroup);
        List<String> orgCodes = dataGridMapper.getOrgCodeByOrgGroup(param);
        Map<String,Object> dataGridParam = new HashMap<>();
        dataGridParam.put("weightY",year);
        for(Map<String,Object> rowData : datagridVO.getValues()){
            dataGridParam.put("indexCode",rowData.get("indexCode").toString());
            Iterator it = rowData.keySet().iterator();
            while (it.hasNext()) {
                String columKey = it.next().toString();
                if(orgCodes.contains(columKey)){
                    dataGridParam.put("orgCode",columKey);
                    TkIndexWeight tkIndexWeight = JSON.parseObject(JSON.toJSONString(dataGridParam), TkIndexWeight.class);
                    QueryWrapper<TkIndexWeight> queryWrapperSdf = new QueryWrapper<>();
                    queryWrapperSdf.lambda().eq(TkIndexWeight::getIndexCode, tkIndexWeight.getIndexCode()).eq(TkIndexWeight::getWeightY, tkIndexWeight.getWeightY()).eq(TkIndexWeight::getOrgCode, tkIndexWeight.getOrgCode());
                    TkIndexWeight oldTkIndexWeight = weightMapper.selectOne(queryWrapperSdf);
                    if(oldTkIndexWeight.getWeightFormat()==1){  //判断百分比

                        boolean contains = rowData.get(columKey).toString().contains("%");
                        if(!contains){
                            rowData.put(columKey,rowData.get(columKey).toString()+"%");
                        }else{
                            rowData.put(columKey,rowData.get(columKey).toString().replace("%","")+"%");

                        }
                    }
                    if(oldTkIndexWeight.getWeightFormat()==2){	//判断是区间
                        boolean contains = rowData.get(columKey).toString().contains("[");
                        if(!contains){
                            rowData.put(columKey,"["+rowData.get(columKey).toString()+"]");
                        }
                    }
                    dataGridParam.put("WEIGHT_M"+month,rowData.get(columKey));
                    tkIndexWeight = JSON.parseObject(JSON.toJSONString(dataGridParam), TkIndexWeight.class);
                    weightMapper.update(tkIndexWeight,queryWrapperSdf);
                    try {
                        MyBeanUtils.copyBeanNotNull2Bean(tkIndexWeight, oldTkIndexWeight);
                        TkIndexWeightLog entityLog = new TkIndexWeightLog();
                        MyBeanUtils.copyBeanNotNull2Bean(oldTkIndexWeight, entityLog);
                        entityLog.setweightId(oldTkIndexWeight.getId());
                        entityLog.setId(null);
                        weightLogMapper.insert(entityLog);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        Map<String,Object> rowData = datagridVO.getValues().get(0);
        Iterator it = rowData.keySet().iterator();
        Map<String,Object> weightParam = new HashMap<>();
        weightParam.put("weightY",year);
        while (it.hasNext()) {
            String columKey = it.next().toString();
            if(orgCodes.contains(columKey)){
                weightParam.put("orgCode",columKey);
                List<TkIndexWeight> tkIndexWeights = weightMapper.selectSumWeightForXiaoJi(weightParam);
                TkIndexWeight zongJiWeight = new TkIndexWeight();
                zongJiWeight.setWeightY(year);
                zongJiWeight.setOrgCode(columKey);
                zongJiWeight.setIndexCode("IZZZ999");
                zongJiWeight.setWeightM01(DoubleUtil.add(Double.valueOf(tkIndexWeights.get(0).getWeightM01().replace("%","")),Double.valueOf(tkIndexWeights.get(1).getWeightM01().replace("%","")))+"%");
                zongJiWeight.setWeightM02(DoubleUtil.add(Double.valueOf(tkIndexWeights.get(0).getWeightM02().replace("%","")),Double.valueOf(tkIndexWeights.get(1).getWeightM02().replace("%","")))+"%");
                zongJiWeight.setWeightM03(DoubleUtil.add(Double.valueOf(tkIndexWeights.get(0).getWeightM03().replace("%","")),Double.valueOf(tkIndexWeights.get(1).getWeightM03().replace("%","")))+"%");
                zongJiWeight.setWeightM04(DoubleUtil.add(Double.valueOf(tkIndexWeights.get(0).getWeightM04().replace("%","")),Double.valueOf(tkIndexWeights.get(1).getWeightM04().replace("%","")))+"%");
                zongJiWeight.setWeightM05(DoubleUtil.add(Double.valueOf(tkIndexWeights.get(0).getWeightM05().replace("%","")),Double.valueOf(tkIndexWeights.get(1).getWeightM05().replace("%","")))+"%");
                zongJiWeight.setWeightM06(DoubleUtil.add(Double.valueOf(tkIndexWeights.get(0).getWeightM06().replace("%","")),Double.valueOf(tkIndexWeights.get(1).getWeightM06().replace("%","")))+"%");
                zongJiWeight.setWeightM07(DoubleUtil.add(Double.valueOf(tkIndexWeights.get(0).getWeightM07().replace("%","")),Double.valueOf(tkIndexWeights.get(1).getWeightM07().replace("%","")))+"%");
                zongJiWeight.setWeightM08(DoubleUtil.add(Double.valueOf(tkIndexWeights.get(0).getWeightM08().replace("%","")),Double.valueOf(tkIndexWeights.get(1).getWeightM08().replace("%","")))+"%");
                zongJiWeight.setWeightM09(DoubleUtil.add(Double.valueOf(tkIndexWeights.get(0).getWeightM09().replace("%","")),Double.valueOf(tkIndexWeights.get(1).getWeightM09().replace("%","")))+"%");
                zongJiWeight.setWeightM10(DoubleUtil.add(Double.valueOf(tkIndexWeights.get(0).getWeightM10().replace("%","")),Double.valueOf(tkIndexWeights.get(1).getWeightM10().replace("%","")))+"%");
                zongJiWeight.setWeightM11(DoubleUtil.add(Double.valueOf(tkIndexWeights.get(0).getWeightM11().replace("%","")),Double.valueOf(tkIndexWeights.get(1).getWeightM11().replace("%","")))+"%");
                zongJiWeight.setWeightM12(DoubleUtil.add(Double.valueOf(tkIndexWeights.get(0).getWeightM12().replace("%","")),Double.valueOf(tkIndexWeights.get(1).getWeightM12().replace("%","")))+"%");
                QueryWrapper<TkIndexWeight> xiaoJi1 = new QueryWrapper<>();
                xiaoJi1.lambda().eq(TkIndexWeight::getWeightY,year);
                xiaoJi1.lambda().eq(TkIndexWeight::getOrgCode,tkIndexWeights.get(0).getOrgCode());
                xiaoJi1.lambda().eq(TkIndexWeight::getIndexCode,tkIndexWeights.get(0).getIndexCode());
                weightMapper.update(tkIndexWeights.get(0),xiaoJi1);
                QueryWrapper<TkIndexWeight> xiaoJi2 = new QueryWrapper<>();
                xiaoJi2.lambda().eq(TkIndexWeight::getWeightY,year);
                xiaoJi2.lambda().eq(TkIndexWeight::getOrgCode,tkIndexWeights.get(1).getOrgCode());
                xiaoJi2.lambda().eq(TkIndexWeight::getIndexCode,tkIndexWeights.get(1).getIndexCode());
                weightMapper.update(tkIndexWeights.get(1),xiaoJi2);
                QueryWrapper<TkIndexWeight> zongji = new QueryWrapper<>();
                zongji.lambda().eq(TkIndexWeight::getWeightY,year);
                zongji.lambda().eq(TkIndexWeight::getOrgCode,zongJiWeight.getOrgCode());
                zongji.lambda().eq(TkIndexWeight::getIndexCode,zongJiWeight.getIndexCode());
                weightMapper.update(zongJiWeight,zongji);
            }
        }
        return Result.ok("操作成功");
    }

    /**
     **绩效汇总评分-评分列表（绩效管理员评分）
     * @param
     * @return
     */
    @Override
    public Result<?> kaGradeDatagrid(TkIndexValueForScoreVO tkIndexValue, Integer pageNo, Integer pageSize, LoginUser loginUser,String dataType) {
        Map<String, Object> queryTotalGrade = new HashMap<>();
        int count = 0;
        queryTotalGrade.put("min", (pageNo - 1) * pageSize);
        queryTotalGrade.put("max", pageSize);
        String month = "";
        String yearmonth = tkIndexValue.getIndexYearMonth();
        if (null == yearmonth || "".equals(yearmonth)) {
            return Result.ok(null);
        }
        queryTotalGrade.put("year", yearmonth.substring(0, 4));
        month = yearmonth.substring(4);
        queryTotalGrade.put("month", month);
        queryTotalGrade.put("dataType", dataType);
        if(com.baomidou.mybatisplus.core.toolkit.StringUtils.isNotEmpty(tkIndexValue.getIndexGroup())){
            queryTotalGrade.put("indexGroup", "A01A01A01".equalsIgnoreCase(tkIndexValue.getIndexGroup()) ? "C" : "G");
        }
        queryTotalGrade.put("indexDepart", tkIndexValue.getIndexDepart());
        List<TkIndexValueForScoreVO> totalGradeList = new ArrayList<>();
        List<String> orgSorts = new ArrayList<>();
        String currentYearMonth = EsDateUtils.getIndexYearMonth(new Date());
        queryTotalGrade.put("column",tkIndexValue.getColumn());
        queryTotalGrade.put("order",tkIndexValue.getOrder());
        //sortType==grade  排序根据镜像（往期考核期）或者当前考核表总计得分降序排列   否则按照部门code升序排序
        if(currentYearMonth.equals(yearmonth)){
            totalGradeList = tkGmMapper.selectTotalGradeList(queryTotalGrade);
            count = tkGmMapper.selectTotalGradeCount(queryTotalGrade);
            orgSorts = tkGmMapper.selectOrgSortByTotalGrade(queryTotalGrade);
        }else{
            queryTotalGrade.put("mirrorCode", "MC"+yearmonth);
            totalGradeList = tkGmMapper.selectMirrorTotalGradeList(queryTotalGrade);
            count = tkGmMapper.selectMirrorTotalGradeCount(queryTotalGrade);
            orgSorts = tkGmMapper.selectOrgSortByMirrorTotalGrade(queryTotalGrade);
        }
        Map<String,Integer> sortMap = new HashMap<>();
        for(int i= 0 ; i < orgSorts.size() ; i++){
            sortMap.put(orgSorts.get(i),i+1);
        }
        Map<String, Object> queryIndexValue = new HashMap<>();
        String[] roleCodes = {"ykgm"};
        List<String> userNames = sysUserMapper.selectUserNameByRoleCodes(roleCodes);
        queryIndexValue.put("indexExecutor",userNames.get(0));
        queryIndexValue.put("indexExecutorDepart",loginUser.getOrgCode());
        queryIndexValue.put("year", yearmonth.substring(0, 4));
        for (int i = 0; i < totalGradeList.size(); i++) {
            // 查询该部门的总分
            totalGradeList.get(i).setTotalGrade(DepartCode.formateForDouble(totalGradeList.get(i).getMTotal()));
            String indexScorer = totalGradeList.get(i).getIndexScorer();
            String indexDepart = totalGradeList.get(i).getIndexDepart();
            queryIndexValue.put("indexScorer", indexScorer);
            queryIndexValue.put("indexDepart", indexDepart);
            QueryWrapper<TkDepartGroup> departGroupQueryWrapper = new QueryWrapper<>();
            departGroupQueryWrapper.lambda().eq(TkDepartGroup::getIndexDepart,indexDepart);

            TkDepartGroup tkDepartGroup = tkDepartGroupMapper.selectOne(departGroupQueryWrapper);
            List<String> infoAttrs = new ArrayList<>();
            infoAttrs.add(InfoAttrUtil.SHUANG_XIANG_PING_FEN);
            List<String> indexValueCodes = departAndInfoTool.getIndexValueCodesByOrgGroup(tkDepartGroup.getIndexGroupCode(),infoAttrs);
            queryIndexValue.put("indexValueCodes", indexValueCodes);
            queryIndexValue.put("dataType", dataType);
            TkIndexValueForScoreVO indexValue = tkGmMapper.selectOneIndexValaue(queryIndexValue);
            totalGradeList.get(i).setId(indexValue.getId());
            totalGradeList.get(i).setSortNum(sortMap.get(indexDepart+"@"+indexScorer));
            totalGradeList.get(i).setIndexValueCode(indexValue.getIndexValueCode());
            totalGradeList.get(i).setIndexValueName(indexValue.getIndexValueName());
            totalGradeList.get(i).setIndexYearMonth(yearmonth);
            totalGradeList.get(i).setIndexScorer(indexValue.getIndexScorer());
            totalGradeList.get(i).setIndexScorerName(indexValue.getIndexScorerName());
            totalGradeList.get(i).setIndexDepartName(indexValue.getIndexDepartName());
            QueryWrapper<TkIndexValueReference> referenceQueryWrapper = new QueryWrapper<>();
            referenceQueryWrapper.lambda().eq(TkIndexValueReference::getIndexDepart,indexDepart);
            referenceQueryWrapper.lambda().eq(TkIndexValueReference::getIndexScorer,indexScorer);
            referenceQueryWrapper.lambda().eq(TkIndexValueReference::getIndexValueCode,indexValue.getIndexValueCode());
            referenceQueryWrapper.lambda().eq(TkIndexValueReference::getIndexYearMonth,yearmonth);
            referenceQueryWrapper.lambda().eq(TkIndexValueReference::getWhich,"supervise");
            List<TkIndexValueReference> tkIndexValueReferences = tkIndexValueReferenceMapper.selectList(referenceQueryWrapper);
            totalGradeList.get(i).setSuperviseGrade(tkIndexValueReferences.size() == 0  ? null : DepartCode.formateForDouble(tkIndexValueReferences.get(0).getValue()));
            totalGradeList.get(i).setGrade(DepartCode.formateForDouble(TkGradeUtil.createGrade(indexValue, month)));
            //当前考核表tk_index_value对应月份数据
            totalGradeList.get(i).setM01(indexValue.getM01() == 0 ? null : DepartCode.formateForDouble(indexValue.getM01()));
            totalGradeList.get(i).setM02(indexValue.getM02() == 0 ? null : DepartCode.formateForDouble(indexValue.getM02()));
            totalGradeList.get(i).setM03(indexValue.getM03() == 0 ? null : DepartCode.formateForDouble(indexValue.getM03()));
            totalGradeList.get(i).setM04(indexValue.getM04() == 0 ? null : DepartCode.formateForDouble(indexValue.getM04()));
            totalGradeList.get(i).setM05(indexValue.getM05() == 0 ? null : DepartCode.formateForDouble(indexValue.getM05()));
            totalGradeList.get(i).setM06(indexValue.getM06() == 0 ? null : DepartCode.formateForDouble(indexValue.getM06()));
            totalGradeList.get(i).setM07(indexValue.getM07() == 0 ? null : DepartCode.formateForDouble(indexValue.getM07()));
            totalGradeList.get(i).setM08(indexValue.getM08() == 0 ? null : DepartCode.formateForDouble(indexValue.getM08()));
            totalGradeList.get(i).setM09(indexValue.getM09() == 0 ? null : DepartCode.formateForDouble(indexValue.getM09()));
            totalGradeList.get(i).setM10(indexValue.getM10() == 0 ? null : DepartCode.formateForDouble(indexValue.getM10()));
            totalGradeList.get(i).setM11(indexValue.getM11() == 0 ? null : DepartCode.formateForDouble(indexValue.getM11()));
            totalGradeList.get(i).setM12(indexValue.getM12() == 0 ? null : DepartCode.formateForDouble(indexValue.getM12()));
            // 查询该指标该部门双向评分所有评分部门对其的评分之和
            Map<String, Object> param = new HashMap<>();
            param.put("indexDepart", indexDepart);
            param.put("indexValueCode", indexValue.getIndexValueCode());
            param.put("indexScorer", totalGradeList.get(i).getIndexScorer());
            param.put("dataType", dataType);
            TkIndexValue sumIndexValue = tkIndexValueMapper.selectSumByParam(param);
            param.put("month", "m"+month);
            Map<String,Double> minAndMaxGrage = tkIndexValueMapper.selectMaxAndMinGrageByParam(param);
            if(sumIndexValue==null || minAndMaxGrage == null) {
                totalGradeList.get(i).setAvegGrade(0.00D);
                totalGradeList.get(i).setMaxGrade(0.00D);
                totalGradeList.get(i).setMinGrade(0.00D);
            } else {
                double gradeTotal = TkGradeUtil.createGrade(sumIndexValue, month);
                totalGradeList.get(i).setAvegGrade(DepartCode.formateForDouble(DoubleUtil.div(gradeTotal, sumIndexValue.getMTotal())));
                totalGradeList.get(i).setMaxGrade(DepartCode.formateForDouble(minAndMaxGrage.get("maxGrade")));
                totalGradeList.get(i).setMinGrade(DepartCode.formateForDouble(minAndMaxGrage.get("minGrade")));
            }
            QueryWrapper<TkIndexValueComments> queryWrapperSdf = new QueryWrapper<>();
            queryWrapperSdf.lambda().eq(TkIndexValueComments::getIndexValueId, totalGradeList.get(i).getId()).eq(TkIndexValueComments::getIndexValueM, month);
           // List<TkIndexValueComments> commentsEntity = tkIndexValueCommentsMapper.selectList(queryWrapperSdf);
            /*if (commentsEntity.size()!= 0) {
                totalGradeList.get(i).setIndexValueComments(commentsEntity.getIndexValueComments());
            }*/

            List<TkIndexValueComments> tkIndexValueCommentsEntitys = tkIndexValueCommentsMapper.selectList(queryWrapperSdf);
            List<ScoreDatagridResponseModel.RecordsBean.ScoreCommentBean> scoreComments=new ArrayList<>();
            if (tkIndexValueCommentsEntitys != null && tkIndexValueCommentsEntitys.size() > 0) {
                for (TkIndexValueComments t : tkIndexValueCommentsEntitys) {
                    ScoreDatagridResponseModel.RecordsBean.ScoreCommentBean scoreCommentBean=new ScoreDatagridResponseModel.RecordsBean.ScoreCommentBean();
                    scoreCommentBean.setTitle(t.getIndexValueCommentTitle());
                    scoreCommentBean.setContent(t.getIndexValueComments());
                    scoreCommentBean.setCode(t.getIndexValueCommentCode());
                    scoreComments.add(scoreCommentBean);
                }
            }
            totalGradeList.get(i).setScoreComments(scoreComments);
        }
        Page<TkIndexValueForScoreVO> finalPage = new Page<TkIndexValueForScoreVO>(pageNo, pageSize);
        finalPage.setTotal(count);
        finalPage.setCurrent(pageNo);
        finalPage.setSize(pageSize);
        int pages = count / pageSize;
        if (count % pageSize > 0) {
            pages += 1;
        }
        finalPage.setPages(pages);
        finalPage.setRecords(totalGradeList);
        return Result.ok(finalPage);
        /*int count = 0;
        String month = "";
        String yearmonth = tkIndexValue.getIndexYearMonth();
        if (null == yearmonth || "".equals(yearmonth)) {
            yearmonth = EsDateUtils.getIndexYearMonth(new Date());
        }
        month = yearmonth.substring(4);

        Map<String, Object> queryIndexValue = new HashMap<>();
        queryIndexValue.put("min", (pageNo - 1) * pageSize);
        queryIndexValue.put("max", pageSize);
        queryIndexValue.put("year", yearmonth.substring(0, 4));
        Map<String,Object> userQueryMap = new HashMap<>();
        userQueryMap.put("roleCode","ykgm");
        List<String> userNames = commonMapper.getUserNameByByParam(userQueryMap);
        queryIndexValue.put("indexExecutor",userNames.get(0));
        List<String> infoAttrs = new ArrayList<>();
        infoAttrs.add(InfoAttrUtil.SHUANG_XIANG_PING_FEN);
        List<String> noContainCodes = departAndInfoTool.getIndexValueCodesByParam(null,infoAttrs);
        queryIndexValue.put("indexValueCodes", noContainCodes);
        queryIndexValue.put("dataType",dataType);
        queryIndexValue.put("noVirtual","noVirtual");

        List<TkIndexValueForScoreVO> indexValues = dataGridMapper.selectIndexValauePage(queryIndexValue);
        count = dataGridMapper.selectIndexValauePageCount(queryIndexValue);
        if(indexValues.size() > 0 ){
            for(int i = 0 ; i < indexValues.size() ; i++ ){
                indexValues.get(i).setIndexYearMonth(yearmonth);
                indexValues.get(i).setGrade(DepartCode.formateForDouble(TkGradeUtil.createGrade(indexValues.get(i), month)));
                indexValues.get(i).setM01(indexValues.get(i).getM01() == 0 ? null : DepartCode.formateForDouble(indexValues.get(i).getM01()));
                indexValues.get(i).setM02(indexValues.get(i).getM02() == 0 ? null : DepartCode.formateForDouble(indexValues.get(i).getM02()));
                indexValues.get(i).setM03(indexValues.get(i).getM03() == 0 ? null : DepartCode.formateForDouble(indexValues.get(i).getM03()));
                indexValues.get(i).setM04(indexValues.get(i).getM04() == 0 ? null : DepartCode.formateForDouble(indexValues.get(i).getM04()));
                indexValues.get(i).setM05(indexValues.get(i).getM05() == 0 ? null : DepartCode.formateForDouble(indexValues.get(i).getM05()));
                indexValues.get(i).setM06(indexValues.get(i).getM06() == 0 ? null : DepartCode.formateForDouble(indexValues.get(i).getM06()));
                indexValues.get(i).setM07(indexValues.get(i).getM07() == 0 ? null : DepartCode.formateForDouble(indexValues.get(i).getM07()));
                indexValues.get(i).setM08(indexValues.get(i).getM08() == 0 ? null : DepartCode.formateForDouble(indexValues.get(i).getM08()));
                indexValues.get(i).setM09(indexValues.get(i).getM09() == 0 ? null : DepartCode.formateForDouble(indexValues.get(i).getM09()));
                indexValues.get(i).setM10(indexValues.get(i).getM10() == 0 ? null : DepartCode.formateForDouble(indexValues.get(i).getM10()));
                indexValues.get(i).setM11(indexValues.get(i).getM11() == 0 ? null : DepartCode.formateForDouble(indexValues.get(i).getM11()));
                indexValues.get(i).setM12(indexValues.get(i).getM12() == 0 ? null : DepartCode.formateForDouble(indexValues.get(i).getM12()));
                // 查询该指标该部门双向评分所有评分部门对其的评分之和
                Map<String, Object> param = new HashMap<>();
                param.put("indexDepart", indexValues.get(i).getIndexDepart());
                param.put("indexValueCode", indexValues.get(i).getIndexValueCode());
                param.put("indexScorer", indexValues.get(i).getIndexScorer());
                param.put("dataType", dataType);
                TkIndexValue sumIndexValue = tkIndexValueMapper.selectSumByParam(param);
                if(sumIndexValue==null) {
                    indexValues.get(i).setAvegGrade(0.00D);
                } else {
                    double gradeTotal = TkGradeUtil.createGrade(sumIndexValue, month);
                    indexValues.get(i).setAvegGrade(DepartCode.formateForDouble(DoubleUtil.div(gradeTotal, sumIndexValue.getMTotal())));
                }
                QueryWrapper<TkIndexValueComments> queryWrapperSdf = new QueryWrapper<>();
                queryWrapperSdf.lambda().eq(TkIndexValueComments::getIndexValueId, indexValues.get(i).getId()).eq(TkIndexValueComments::getIndexValueM, month);
                TkIndexValueComments commentsEntity = tkIndexValueCommentsMapper.selectOne(queryWrapperSdf);
                if (commentsEntity != null) {
                    indexValues.get(i).setIndexValueComments(commentsEntity.getIndexValueComments());
                }
            }
        }

        Page<TkIndexValueForScoreVO> finalPage = new Page<TkIndexValueForScoreVO>(pageNo, pageSize);
        finalPage.setTotal(count);
        finalPage.setCurrent(pageNo);
        finalPage.setSize(pageSize);
        int pages = count / pageSize;
        if (count % pageSize > 0) {
            pages += 1;
        }
        finalPage.setPages(pages);
        finalPage.setRecords(indexValues);
        return Result.ok(finalPage);*/
    }

    @Override
    public Result<?> kaMgrAudit(TkIndexValueForScoreVO indexValueForScoreVO,String dataType) {
        String yearMonth = indexValueForScoreVO.getIndexYearMonth();
        String indexGroup = indexValueForScoreVO.getIndexGroup();
        if(com.baomidou.mybatisplus.core.toolkit.StringUtils.isEmpty(yearMonth)) {
            yearMonth = EsDateUtils.getIndexYearMonth(new Date());
        }
        if(StringUtils.isEmpty(indexGroup)){
            return Result.error("参数缺失");
        }
        Map<String,Object> param = new HashMap<>();
        param.put("dataType",dataType);
        param.put("year",yearMonth.substring(0,4));
        if(yearMonth.length() == 6){
            param.put("month",yearMonth.substring(4));
        }else{
            param.put("month",yearMonth.substring(6));
        }
        param.put("indexGroup","C".equals(indexGroup) ? "A01A01A01" : "A01A01A02");
        Map<String,Object> auditMap = dataGridMapper.selectIndexValueAuditByParam(param);
        Map<String,Object> result = new HashMap<>();
        result.put("indexYearMonth",yearMonth);
        result.put("indexAuditStatus",auditMap);
        return Result.ok(result);
    }

    @Override
    public List<Map<String, Object>> getIndexCodeByTarget(String indexGroup,String dataType) {
        Map<String,Object> param=new HashMap<>();
        if(StringUtils.isNotEmpty(indexGroup)){
            String orgGroup = OrgGroupUtil.changeToCG(indexGroup);
            param.put("orgGroup",orgGroup);
        }
        List<String> infoAttrs = new ArrayList<>();
        infoAttrs.add(InfoAttrUtil.MU_BIAO_ZHI);
        List<String> containCodes = departAndInfoTool.getIndexCodesByOrgGroup(indexGroup,infoAttrs);
        param.put("containCodes",containCodes);
        List<Map<String,Object>> result = dataGridMapper.getIndexCodeByTarget(param);
        return result;
    }
}
